'''
Author: your name
Date: 2021-03-26 09:20:19
LastEditTime: 2021-04-20 11:58:27
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: /tinyms/tinyms-api/apps/auth/dict/views.py
'''
from flask import render_template,request
from app import db
from . import auth_dict
from .model import Dict
from .service import DictService

import json
import uuid
import time


#获取字典信息的分页列表
@auth_dict.route('/pages',methods=['GET','POST'])
def pages():
    if request.method == "POST":
        dict_type = request.form.get("dict_type")
        page = request.form.get("page")  #获取页码
    else:
        dict_type = request.form.get("dict_type")
        page = request.args.get("page")
    pages = DictService.pages(dict_type,page) #flask_sqlalchemy提供分页方法.paginate

    data = {
        'msg':'success',
        'code':0,
        'data':Dict.to_dict(pages.items),
        'total':pages.total
    }

    print(pages.items)
    return json.dumps(data,ensure_ascii=False)  #关闭已ascii编码输出




#获取字典信息的一级分页列表
@auth_dict.route('/pagesyj',methods=['GET','POST'])
def pagesyj():
    if request.method == "POST":
        page = request.form.get("page")  #获取页码
    else:
        page = request.args.get("page")
    pages = DictService.pagesyj(page) #flask_sqlalchemy提供分页方法.paginate

    data = {
        'msg':'success',
        'code':0,
        'data':Dict.to_yj_dict(pages.items),
        'total':pages.total
    }

    return json.dumps(data,ensure_ascii=False)  #关闭已ascii编码输出


#保存字典信息
@auth_dict.route('/dictSave',methods=['GET','POST'])
def dictSave():
    # params拿请求
    data = {}
    if request.method == "POST":
        for key in request.form:
            data[key] = request.form.get(key)
    else:
        for key in request.args:
            data[key] = request.args.get(key)
        
    newobj = Dict()
    newobj.dict_code = data['dict_code']
    newobj.dict_name = data['dict_name']
    newobj.type_name = data['type_name']
    newobj.dict_type = data['dict_type']
    newobj.dict_order = data['dict_order']
    newobj.dict_pk = str(uuid.uuid1())
    newobj.dict_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
    newobj.dict_parent = data['dict_parent']
    newobj.dict_remark = data['dict_remark']
    newobj.dict_remark = data['dict_remark']
    newobj.dict_level = data['dict_level']
    newobj.dict_status = data['dict_status']
    DictService.dictSave(newobj)
    data = {
        'msg':'保存成功',
        'code':0,
    }

    return json.dumps(data,ensure_ascii=False)  #关闭已ascii编码输出


#根据主键删除信息
@auth_dict.route('/dictDelete',methods=['GET','POST'])
def dictDelete():
    if request.method == "POST":
        dict_pk = request.form.get("dict_pks")  #获取页码
    else:
        dict_pk = request.args.get("dict_pks")

    DictService.delete(dict_pk)

    data = {
        'msg':'删除成功',
        'code':0,
    }

    return json.dumps(data,ensure_ascii=False)  #关闭已ascii编码输出

#查询状态有效的 字典列表 
@auth_dict.route('/queryDict',methods=['GET','POST'])
def queryDict():
    if request.method == "POST":
        dict_status = request.form.get("dict_status")  #获取页码
    else:
        dict_status = request.args.get("dict_status")

    res = DictService.list(dict_status)
    return json.dumps(Dict.to_dict(res),ensure_ascii=False)  #关闭已ascii编码输出

#查询行政区划
@auth_dict.route('/get_organ_lazy',methods=['GET','POST'])
def get_organ_lazy():
    if request.method == "POST":
        dict_parent = request.form.get("dict_parent")  #获取页码
    else:
        dict_parent = request.args.get("dict_parent")
    dicts = DictService.list_all(dict_parent)
    return json.dumps(Dict.to_dict(dicts),ensure_ascii=False)